package com.np.shop.controller.xhp;


import com.np.shop.entity.TbAdmin;
import com.np.shop.service.ITbAdminService;
import com.np.shop.utils.Result;
import com.np.shop.utils.StatusCode;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 李高帅
 * @since 2020-11-26
 */
@CrossOrigin
@RestController
@RequestMapping("/shop")
public class TbAdminController {
    @Resource
    private ITbAdminService tbAdminService;

    /**
     * 运营商的登录
     * @param tbAdmin
     * @return
     */
    @PostMapping("/login")
    public Result toLogin(@RequestBody TbAdmin tbAdmin){
        TbAdmin tbAdmin1=tbAdminService.findByUsernameAndPassword(tbAdmin);
        if (tbAdmin1 !=null){
            return new Result(StatusCode.SUCCESS,true,"Ok",tbAdmin1);
        }else{
            return new Result(StatusCode.ERROR,false,"失败",null);
        }
    }
    @GetMapping("/list")
    public Result getList(){
        List<TbAdmin> list = tbAdminService.list();
        return new Result(StatusCode.SUCCESS,true,"登陆成功",list);
    }
    @PostMapping("/addAdmin")
    public Result addAdmin(@RequestBody TbAdmin tbAdmin){
        tbAdmin.setCreated(new Date());
        tbAdmin.setStatus(0);
        boolean tbAdmin1= tbAdminService.save(tbAdmin);
        if(tbAdmin1){
            return new Result(StatusCode.SUCCESS,true,"添加成功",null);
        }else{
            return new Result(StatusCode.ERROR,false,"添加失败",null);
        }
    }
    @DeleteMapping("/del/{id}")
    public Result delAd(@PathVariable BigInteger id){
        boolean b = tbAdminService.removeById(id);
        if(b){
            return new Result(StatusCode.SUCCESS,true,"删除成功",null);
        }else{
            return new Result(StatusCode.ERROR,false,"删除失败",null);
        }
    }
    @PutMapping("/update")
    public Result updAd(@RequestBody TbAdmin tbAdmin){
        tbAdminService.update(tbAdmin);
        return new Result(StatusCode.SUCCESS,true,"修改成功",null);
    }
    @GetMapping("/find/{id}")
    public Result findBY(@PathVariable BigInteger id){
        return new Result(StatusCode.SUCCESS,true,"查询成功",tbAdminService.getById(id));
    }
}
